<?php
require_once(dirname(__FILE__).'/../config/db_connect.php');
require_once(dirname(__FILE__).'/../departement/departement.cl.php');

class Service {

    private $error = array();
    private $name;
    private $abreviation;
    private $departementId;
    private $id;

    /**
    * Departement
    */
    private $departement;

    public function __construct() {

        $this->departement = new Departement();
    }

    public function setValues($data) {

       $this->id =  empty($data['id']) ? '' : strip_tags(trim($data['id']));       
       $this->name = empty($data['name']) ? '' : strip_tags(trim($data['name']));
       $this->abreviation =  empty($data['abreviation']) ? '' : strip_tags(trim($data['abreviation']));
       $this->departementId = empty($data['departement_id']) ? '' : strip_tags(trim($data['departement_id']));
    }
    public function reset() {

       $this->id = '';
       $this->name = '';
       $this->abreviation = '';
       $this->departementId = '';
    }

    public function save($data) {
      $this->setValues($data);
        if($this->getId()) {
            if($this->validate()) {
                $update="UPDATE service SET name='$this->name', abreviation='$this->abreviation' , departement_id='$this->departementId' WHERE id='$this->id' ";
                //debug($update);
                mysql_query ($update);
                //return $this->getEmploye(mysql_insert_id());
            }
        } else {
            if($this->validate()) {
            mysql_query ("INSERT INTO service VALUES ('','  " .$this->name. " ', ' " .$this->abreviation. " ' , ' " .$this->departementId. " ')");
            $this->reset();
            //return $this->getService(mysql_insert_id());
            }
  
         }
 }
        

    public function loadServiceByID($id) {
        $select = "SELECT * FROM service WHERE id = '$id'";
        $resource = mysql_query($select);
        $this->setValues(mysql_fetch_array($resource));
    }

    public function getAllServices() {
        
        $select = "SELECT * FROM service";
        $resource = mysql_query($select);

        $services = array();
        while($service = mysql_fetch_object($resource)) {
              $services[] = $service;
        } 
        return $services; 
    }

    public function getServiceByDepartementId($departementId) {
        
        $select = "SELECT * FROM service " ;
        $select .= $departementId == 0 ? '' : "WHERE departement_id='$departementId'";
        $resource = mysql_query($select);
        $services = array();
        while($service = mysql_fetch_object($resource)) {
              $services[] = $service;
        } 
        return $services; 
    }

    private function validate() {
        
        if (empty($this->departementId)) {
             $this->addError("departement_id","Selectionner un departement");
        }
        if (empty($this->name)) {
             $this->addError("name","Saisissez le nom");
        }
        if (empty($this->abreviation)) {
             $this->addError("abreviation","Saisissez l'abreviation");
        }
        if(count($this->error) > 0) {
            return false;
        } else {
            return true;
        }      
    }

   private function addError($index, $error) {
        $this->error[$index] =  $error;
    }

    public function getError($index) { 
      if(!empty($this->error[$index])) {
        return '<div class="alert alert-error">'.$this->error[$index].'</div>';
      }
    }
    public function hasError() {
       return count($this->error) > 0 ? true : false;
    }

    public function getName() {
        
        return $this->name;
    }

    public function getAbreviation() {

        return $this->abreviation;
    }

    public function getDepartementId() {

        return $this->departementId;
    }
    public function getId(){
        return $this->id;
    }

    public function getDepartement(){
        return $this->departement;
    }
}
